$(function(){
    $(document).bind("contextmenu",function(e){   
        return false;   
   	});
	var $doc = $(document);
	var params;
	$doc.on("clearData",function(){
		$("td[style='color: rgb(255, 0, 0);']").text("");
		$('td').off("click");
		$(".elem_type_box").css('display', "none");
	});
	$doc.on("refreshPage",function(){
		window.location.reload();
	});
	//取消
    $('#cancel').click(function(){
    	$("#dataTable tbody").html("");
    	$("#dsddrag1479644438727").hide();
    });
    // 重置金额
    $('#reset').click(function(){
    	$("#amtPay").val("");
    });
    // 选中
    $('td').click(function(){
    	var playtype = $(this).attr("playtype");
    	var dictcode = $(this).attr("dictcode");
    	$("td[playtype="+playtype+"][dictcode="+dictcode+"]").toggleClass('onBg');
    	var groupNum = 0;
    	var selectTd = $("td[class='fontBlue ballno-t-t onBg']");
    	selectTd.each(function(){
			 groupNum++;
	    });
	    if(groupNum > 0){
	    	$('#selectedAmount').html(groupNum);
	    	$(".bulk-amount-times").show(); 
	    }else{
	    	$(".bulk-amount-times").hide(); 
	    }
    });
    //确定投注
    $('#determine').click(function(){
    	$("#dataTable tbody").html("");
    	$("#dsddrag1479644438727").hide();
    	var balance = $("#userBalance").val();
    	var money = $("#totalAmount").html();
    	if(Number(money) > Number(balance)){
    		layer.msg("余额不足");return;
    	}
    	$.ajax({
	    	url:basePath+"cathectic",type:"POST",data:{"catecticArr":params},
	    	success:function(data){
	    		layer.msg(data.msg);
	    		$("#kuaijie_div td[class='onBg']").toggleClass('onBg');
	    		$("#kuaijie_div td[class='fontBlue onBg']").toggleClass('onBg');
	    		$("#amtPay").attr("value","");
	    		dataAjax();
	    	}
	    });
    });
    // 投注确认
    $('#submit').click(function(){
    	var amtPay = $("#amtPay").val();
    	if(amtPay.trim()=="" || amtPay ==null){
    		layer.msg("您输入类型不正确或没有输入实际金额");return;
    	}else if(Number(amtPay) == 0){
    		layer.msg("投注金额必须大于等于1");return;
    	}
    	var html = "",sumAmt=0,groupNum=0;
    	$("td[class='fontBlue onBg']").each(function(){
		 	var playtype = $(this).attr("playtype");
		 	var number = $(this).attr("number");
		 	var dictcode = $(this).attr("dictcode");
		 	var head = $(this).parents().find("th").html();
		 	if($.trim(head).indexOf("和") > 0){
		 		head = "冠亚和";
		 	}
		 	var playTypeName = $(this).html();
		 	if(parseInt(dictcode) <= 10){
		 		playTypeName = parseInt(dictcode);
		 	}
		 	html = html + "<tr><td colspan='16'>" + head + playTypeName+"</td><td colspan='16' class='red'>"+number+
		 	"</td>"+"</td><td colspan='16'>"+amtPay+"</td>"+
		 	"<td style='display: none;' name='cathecticInt'>"+amtPay+"</td>"
		 	+"<td style='display: none;' name='cathecticType'>"+playtype+"</td>"
		 	+"<td style='display: none;' name='cathecticCode'>"+dictcode+"</td>"
		 	+"</tr>";
		 	sumAmt = Number(sumAmt)+Number(amtPay);
		 	groupNum++;
        });
	    if(groupNum == 0){
	    	layer.msg("您未选择号码！");return;
	    }
	    //注数
	    $('#selectedAmount').html(groupNum);
	    $(".bulk-amount-times").show();
	    $("span[id=groupNum]").html(groupNum);
	    $("span[id=totalAmount]").html(sumAmt);
	    $(".orderList").append(html);
	    $("#dsddrag1479644438727").show();
	    params = getJson("#dataTable .orderList tr");
    });
});
var wait = 10,timeclose=0,timeopen=0;
function timeOut(){  
    setTimeout(function(){  
        wait--;
        if(wait==0){  
        	wait = 10;  
    	}    
        $('#autoRefreshspan1').text(wait);  
        timeOut();  
    },1000); 
}
function cleanTimeOut(){
	window.clearTimeout(t);
}
function formatSeconds(value) {
    var theTime = parseInt(value);// 秒  
    var theTime1 = 0;// 分  
    var theTime2 = 0;// 小时  
    if(theTime > 60) {  
        theTime1 = parseInt(theTime/60);  
        theTime = parseInt(theTime%60);  
        if(theTime1 > 60) {  
            theTime2 = parseInt(theTime1/60);  
            theTime1 = parseInt(theTime1%60);  
         }  
    }  
    var result = ""+parseInt(theTime)+"秒";  
    if(theTime1 > 0) {  
    	result = ""+parseInt(theTime1)+"分"+result;  
    }  
    if(theTime2 > 0) {  
    	result = ""+parseInt(theTime2)+"小时"+result;  
    }  
    return result;  
}

function getJson(id){
	var list="[";
	$(id).each(function(i,item){
		var obj = "{";
		$(this).find("td").each(function(x,item){
			var key = $(this).attr("name");
			if(key==null || key==''){
				return true;
			}
			var content = $(this).html();
			obj = obj+'"'+key+'"'+":"+'"'+content+'"'+",";
		});
		obj=obj+"}";
		var index = obj.lastIndexOf(",");
		obj = obj.substring(0,index)+"}";
		list=list+obj+",";
	});
	list = list.substring(0,list.length-1)+"]";
	return list;
}  
function dataAjax(){
	    $.ajax({
		    url:basePath+"cathectic/latest_cathectic",dataType:"json",async:true,data:{"t":Math.random()},type:"GET",
		    success:function(req){
		        if(req.resultCode == '5'){
					layer.msg("登录已失效，请重新登录");
					return;
				}
				var all_amt = req.data.cathecticInt;
		        if(all_amt > 0){
		        	$("#total_amount1").html(all_amt);
		        }
				$("#openData").html("");
		    	$.each(req.dataList, function(i, item) {
		            $("#openData").append("<tr><td>"+item.cathecticCodeStr+"</td><td class='red'>"+item.dictRate+"</td><td>"+item.cathecticInt+"</td><td>"+item.dateTime+"</td></tr>");
		        });
		    }
		});
		$.ajax({
		    url:basePath+"user/refresh_user",dataType:"json",async:true,data:{"t":Math.random()},type:"GET",
		    success:function(req){
		        $("#re_credit").html(req.data.userBalance);
		        $("#win").html(req.data.todayWin);
		    },error:function(){
		    	layer.msg("未知异常");
		    }
		});
}
//会员修改密码
function vipUpdatePassword(){
	var passwordRepeat = $("input[name=passwordRepeat]").val();
	var oldPassword = $("input[name=oldPassword]").val();
	var password = $("input[name=password]").val();
	if(oldPassword.trim() == ''){
		layer.msg("原密码不能为空");
		return;
	}
	if(password.trim() == ''){
		layer.msg("新密码不能为空");
		return;
	}
	if(passwordRepeat.trim() == ''){
		layer.msg("确认密码不能为空");
		return;
	}
	if(password != passwordRepeat){
		layer.msg("新密码与确认密码不一致");
		return;
	}
	$.ajax({
        type: "POST",
        url:$("form").attr("action"),
        data:$("form").serialize(),
        error: function(request) {
            layer.msg("未知异常");
        },
        success: function(data) {
        	if(data.resultCode == '5'){
        		window.location.href = basePath;
        	}else if(data.resultCode == '0'){
        		if(confirm("修改成功，是否确认重新登陆？")){
        			window.location.href = basePath + "user/login";
        		}else{
        			window.location.reload();
        		} 
        	}
        }
    });
}